﻿using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;
using XDYRestaurant.Models;

#nullable disable

namespace XDYRestaurant.Context
{
    public partial class Context : DbContext
    {
        public Context()
        {
        }

        public Context(DbContextOptions<Context> options)
            : base(options)
        {
        }

        public virtual DbSet<Admin> Admins { get; set; }
        public virtual DbSet<Bill> Bills { get; set; }
        public virtual DbSet<Cookbook> Cookbooks { get; set; }
        public virtual DbSet<Customer> Customers { get; set; }
        public virtual DbSet<Order> Orders { get; set; }
        public virtual DbSet<Table> Tables { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            if (!optionsBuilder.IsConfigured)
            {
                optionsBuilder.UseSqlServer("Data Source=.;User ID=sa;pwd=as;Initial Catalog=Restaurant;");
            }
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.HasAnnotation("Relational:Collation", "Chinese_PRC_CI_AS");

            modelBuilder.Entity<Admin>(entity =>
            {
                entity.HasKey(e => e.AdminPhone);

                entity.ToTable("Admin");

                entity.Property(e => e.AdminPhone).HasMaxLength(11);

                entity.Property(e => e.AdminPassword)
                    .IsRequired()
                    .HasMaxLength(50);

                entity.Property(e => e.AdminProfile).HasColumnType("ntext");
            });

            modelBuilder.Entity<Bill>(entity =>
            {
                entity.ToTable("Bill");

                entity.Property(e => e.BillTime).HasColumnType("datetime");

                entity.Property(e => e.CookId)
                    .IsRequired()
                    .HasMaxLength(6);

                entity.Property(e => e.CustomerPhone)
                    .IsRequired()
                    .HasMaxLength(11);

                entity.HasOne(d => d.Cook)
                    .WithMany(p => p.Bills)
                    .HasForeignKey(d => d.CookId)
                    .OnDelete(DeleteBehavior.ClientSetNull)
                    .HasConstraintName("FK_Bill_Cookbook");

                entity.HasOne(d => d.CustomerPhoneNavigation)
                    .WithMany(p => p.Bills)
                    .HasForeignKey(d => d.CustomerPhone)
                    .OnDelete(DeleteBehavior.ClientSetNull)
                    .HasConstraintName("FK_Bill_Customer");

                entity.HasOne(d => d.Table)
                    .WithMany(p => p.Bills)
                    .HasForeignKey(d => d.TableId)
                    .OnDelete(DeleteBehavior.ClientSetNull)
                    .HasConstraintName("FK_Bill_Table");
            });

            modelBuilder.Entity<Cookbook>(entity =>
            {
                entity.HasKey(e => e.CookId)
                    .HasName("PK_cookbook");

                entity.ToTable("Cookbook");

                entity.Property(e => e.CookId).HasMaxLength(6);

                entity.Property(e => e.CookName)
                    .IsRequired()
                    .HasMaxLength(15);

                entity.Property(e => e.CookSpecies).HasMaxLength(15);
            });

            modelBuilder.Entity<Customer>(entity =>
            {
                entity.HasKey(e => e.CustomerPhone);

                entity.ToTable("Customer");

                entity.Property(e => e.CustomerPhone).HasMaxLength(11);

                entity.Property(e => e.CustomerName).HasMaxLength(50);

                entity.Property(e => e.CustomerPassword)
                    .IsRequired()
                    .HasMaxLength(50);

                entity.Property(e => e.CustomerProfile).HasColumnType("ntext");

                entity.HasOne(d => d.Table)
                    .WithMany(p => p.Customers)
                    .HasForeignKey(d => d.TableId)
                    .HasConstraintName("FK_Customer_Table");
            });

            modelBuilder.Entity<Order>(entity =>
            {
                entity.ToTable("Order");

                entity.Property(e => e.CookId)
                    .IsRequired()
                    .HasMaxLength(6);

                entity.Property(e => e.CustomerPhone)
                    .IsRequired()
                    .HasMaxLength(11);

                entity.HasOne(d => d.Cook)
                    .WithMany(p => p.Orders)
                    .HasForeignKey(d => d.CookId)
                    .OnDelete(DeleteBehavior.ClientSetNull)
                    .HasConstraintName("FK_Table_Cookbook");

                entity.HasOne(d => d.CustomerPhoneNavigation)
                    .WithMany(p => p.Orders)
                    .HasForeignKey(d => d.CustomerPhone)
                    .OnDelete(DeleteBehavior.ClientSetNull)
                    .HasConstraintName("FK_Table_Customer");

                entity.HasOne(d => d.Table)
                    .WithMany(p => p.Orders)
                    .HasForeignKey(d => d.TableId)
                    .OnDelete(DeleteBehavior.ClientSetNull)
                    .HasConstraintName("FK_Order_Table");
            });

            modelBuilder.Entity<Table>(entity =>
            {
                entity.ToTable("Table");

                entity.Property(e => e.TableId).ValueGeneratedNever();

                entity.Property(e => e.CustomerPhone).HasMaxLength(11);

                entity.Property(e => e.TableStatic).HasDefaultValueSql("((0))");

                entity.HasOne(d => d.CustomerPhoneNavigation)
                    .WithMany(p => p.Tables)
                    .HasForeignKey(d => d.CustomerPhone)
                    .HasConstraintName("FK_Table_Customer1");
            });

            OnModelCreatingPartial(modelBuilder);
        }

        partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
    }
}
